﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Main_Data;
using DevExpress.XtraReports.UI;
using System.IO;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.Preview;
using DevExpress.XtraReports.Extensions;


namespace Print_UI
{
    public partial class FrmCreateTemplate : DevExpress.XtraEditors.XtraForm
    {
        public FrmCreateTemplate()
        {
            InitializeComponent();
        }

        public dsReport Data { get; set; }
        public byte[] TemplateStream { get; set; }
        public bool IsChanged { get; set; }
        private CommandParams _cmd = new CommandParams();

        private void FrmCreateTemplate_Load(object sender, EventArgs e)
        {
            try
            {
                string pathTemp = Application.StartupPath + "\\temp.repx";
                string pathTempSave = Application.StartupPath + "\\tempSave.repx";
                string pathTempSaveCopy = Application.StartupPath + "\\tempSaveCopy.repx";

                _cmd.PathTemp = pathTemp;
                _cmd.PathTempSave = pathTempSave;
                _cmd.PathTempSaveCopy = pathTempSaveCopy;
                _cmd.Data = Data;
                _cmd.IsChanged = false;

                if (System.IO.File.Exists(_cmd.PathTemp))
                    System.IO.File.Delete(_cmd.PathTemp);
                if (System.IO.File.Exists(_cmd.PathTempSave))
                    System.IO.File.Delete(_cmd.PathTempSave);
                if (System.IO.File.Exists(_cmd.PathTempSaveCopy))
                    System.IO.File.Delete(_cmd.PathTempSaveCopy);
                IsChanged = false;

                if (TemplateStream != null)
                {
                    System.IO.FileStream fs = new System.IO.FileStream(_cmd.PathTemp, System.IO.FileMode.OpenOrCreate);
                    fs.Write(TemplateStream, 0, TemplateStream.Length);
                    fs.Close();
                }
                else
                {
                    Template rpTemp = new Template();
                    rpTemp.SaveLayout(_cmd.PathTemp);
                }
                Template rp = new Template();
                try
                {
                    rp.LoadLayout(_cmd.PathTemp);
                }
                catch { }
                rp.DataSource = Data;
                xrDesignMdiController1.OpenReport(rp);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void xrDesignMdiController1_DesignPanelLoaded(object sender, DevExpress.XtraReports.UserDesigner.DesignerLoadedEventArgs e)
        {
            DevExpress.XtraReports.UserDesigner.XRDesignPanel panel = (DevExpress.XtraReports.UserDesigner.XRDesignPanel)sender;
            xrDesignMdiController1.AddCommandHandler(new SaveCommandHandler(panel, _cmd));
            xrDesignMdiController1.AddCommandHandler(new OpenCommandHandler(panel, _cmd));
            xrDesignMdiController1.AddCommandHandler(new CloseCommandHandler(panel, _cmd));
        }

        public class SaveCommandHandler : DevExpress.XtraReports.UserDesigner.ICommandHandler
        {
            private DevExpress.XtraReports.UserDesigner.XRDesignPanel _panel;
            private CommandParams _cmd;

            public SaveCommandHandler(DevExpress.XtraReports.UserDesigner.XRDesignPanel panel, CommandParams cmd)
            {
                this._panel = panel;
                this._cmd = cmd;
            }

            public void HandleCommand(DevExpress.XtraReports.UserDesigner.ReportCommand command, object[] args)
            {
                try
                {
                    Save();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            public bool CanHandleCommand(DevExpress.XtraReports.UserDesigner.ReportCommand command, ref bool useNextHandler)
            {
                useNextHandler = !(command == DevExpress.XtraReports.UserDesigner.ReportCommand.SaveFile ||
                    command == DevExpress.XtraReports.UserDesigner.ReportCommand.SaveFileAs ||
                    command == DevExpress.XtraReports.UserDesigner.ReportCommand.Closing);
                return !useNextHandler;
            }

            void Save()
            {
                _panel.Report.SaveLayout(_cmd.PathTempSave);
                System.IO.File.Copy(_cmd.PathTempSave, _cmd.PathTempSaveCopy, true);
                _cmd.IsChanged = true;

                _panel.ReportState = DevExpress.XtraReports.UserDesigner.ReportState.Saved;
            }
        }

        public class OpenCommandHandler : DevExpress.XtraReports.UserDesigner.ICommandHandler
        {
            private DevExpress.XtraReports.UserDesigner.XRDesignPanel _panel;
            private CommandParams _cmd;

            public OpenCommandHandler(DevExpress.XtraReports.UserDesigner.XRDesignPanel panel, CommandParams cmd)
            {
                this._panel = panel;
                this._cmd = cmd;
            }

            public void HandleCommand(DevExpress.XtraReports.UserDesigner.ReportCommand command, object[] args)
            {
                try
                {
                    Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            public bool CanHandleCommand(DevExpress.XtraReports.UserDesigner.ReportCommand command, ref bool useNextHandler)
            {
                useNextHandler = !(command == DevExpress.XtraReports.UserDesigner.ReportCommand.OpenFile);
                return !useNextHandler;
            }

            void Open()
            {
                bool flag = false;
                if (_cmd.IsChanged)
                {
                    if (MessageBox.Show("Do you want reset all ? You will lost all changed", "OPEN", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        flag = true;
                }
                else
                {
                    if (_panel.ReportState == DevExpress.XtraReports.UserDesigner.ReportState.Changed)
                        if (MessageBox.Show("Do you want reset all ? You will lost all changed", "OPEN", MessageBoxButtons.YesNo) == DialogResult.Yes)
                            flag = true;
                }
                if (flag)
                {
                    Template rp = new Template();
                    try
                    {
                        rp.LoadLayout(_cmd.PathTemp);
                    }
                    catch { }
                    rp.DataSource = _cmd.Data;
                    _panel.OpenReport(rp);
                    _cmd.IsChanged = false;

                    if (System.IO.File.Exists(_cmd.PathTempSave))
                        System.IO.File.Delete(_cmd.PathTempSave);
                    if (System.IO.File.Exists(_cmd.PathTempSaveCopy))
                        System.IO.File.Delete(_cmd.PathTempSaveCopy);
                }

                _panel.ReportState = DevExpress.XtraReports.UserDesigner.ReportState.Opened;
            }
        }

        public class CloseCommandHandler : DevExpress.XtraReports.UserDesigner.ICommandHandler
        {
            private DevExpress.XtraReports.UserDesigner.XRDesignPanel _panel;
            private CommandParams _cmd;

            public CloseCommandHandler(DevExpress.XtraReports.UserDesigner.XRDesignPanel panel, CommandParams cmd)
            {
                this._panel = panel;
                this._cmd = cmd;
            }

            public void HandleCommand(DevExpress.XtraReports.UserDesigner.ReportCommand command, object[] args)
            {
                try
                {
                    Exit();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            public bool CanHandleCommand(DevExpress.XtraReports.UserDesigner.ReportCommand command, ref bool useNextHandler)
            {
                useNextHandler = !(command == DevExpress.XtraReports.UserDesigner.ReportCommand.Close ||
                    command == DevExpress.XtraReports.UserDesigner.ReportCommand.Exit);
                return !useNextHandler;
            }

            void Exit()
            {
                if (_panel.ReportState == DevExpress.XtraReports.UserDesigner.ReportState.Changed)
                {
                    if (MessageBox.Show("Do you want save changed?", "SAVE", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        _panel.Report.SaveLayout(_cmd.PathTempSave);
                        System.IO.File.Copy(_cmd.PathTempSave, _cmd.PathTempSaveCopy, true);
                        _cmd.IsChanged = true;
                    }
                    _panel.ReportState = DevExpress.XtraReports.UserDesigner.ReportState.None;
                    Form f = (Form)this._panel.Parent.Parent.Parent;
                    f.Close();
                }
            }
        }

        private void FrmCreateTemplate_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (_cmd.IsChanged && System.IO.File.Exists(_cmd.PathTempSaveCopy))
                {
                    System.IO.FileStream fs = new System.IO.FileStream(_cmd.PathTempSaveCopy, System.IO.FileMode.Open);
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, (int)fs.Length);
                    TemplateStream = buffer;
                    fs.Close();
                    IsChanged = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}